package cn.baddonkey.ui;

import cn.baddonkey.util.TransApi;
import cn.baddonkey.util.TransResult;
import com.google.gson.Gson;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author junheng.zhou
 * @since 2022-06-28 10:08
 **/
public class MainDialog extends JDialog {

    private JTextField textField;

    private Editor editor;

    private Project project;

    private Document document;

    private Caret currentCaret;

    public MainDialog(Editor editor, Project project) {
        this.editor = editor;
        this.project = project;
        this.document = editor.getDocument();
        this.currentCaret = editor.getCaretModel().getCurrentCaret();

        textField = new JTextField();
        textField.setColumns(30);
        textField.requestFocusInWindow();
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar()==KeyEvent.VK_ENTER) {
                    generateVarName();
                    MainDialog.this.setVisible(false);
                }
            }
        });

        JPanel jPanel = new JPanel();
        jPanel.add(textField);
        setContentPane(jPanel);

        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) screensize.getWidth() / 2 - this.getWidth() / 2;
        int y = (int) screensize.getHeight() / 2 - this.getWidth() / 2;
        this.setLocation(x, y);
        this.pack();
        this.setVisible(true);
    }

    private void generateVarName() {
        Gson gson = new Gson();
        String text = textField.getText();
        if (StringUtils.isBlank(text)) {
            return;
        }
        TransApi api = new TransApi();
        try {
            String transResultJSON = api.getTransResult(text, "auto", "en");

            TransResult transResult = gson.fromJson(transResultJSON, TransResult.class);
            String varName = trans2Hump(transResult.getTrans_result().get(0).getDst());
            WriteCommandAction.runWriteCommandAction(project, new Runnable() {
                @Override
                public void run() {
                    int currentOffSet = currentCaret.getOffset();
                    document.insertString(currentOffSet, varName);
                    editor.getCaretModel().moveToOffset(currentOffSet + varName.length());
                }
            });

        } catch (Exception e) {
            WriteCommandAction.runWriteCommandAction(project, new Runnable() {
                @Override
                public void run() {
                    int currentOffSet = currentCaret.getOffset();
                    document.insertString(currentOffSet, e.getMessage());
                    editor.getCaretModel().moveToOffset(currentOffSet + e.getMessage().length());
                }
            });
            e.printStackTrace();
        }
    }

    private String trans2Hump(String transResult) {
        String[] split = transResult.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : split) {
            stringBuilder.append(firstLetterName(s));
        }
        return firstLowerName(stringBuilder.toString());
    }


    /**
     * 首字母大写
     * @param name
     * @return
     */
    private String firstLetterName(String name) {
        char[] cs=name.toCharArray();
        if (!(cs[0] >= 'a' && cs[0] <= 'z')) {
            return name;
        }
        cs[0]-=32;
        return String.valueOf(cs);
    }

    /**
     * 首字母小写
     * @param name
     * @return
     */
    private String firstLowerName(String name) {
        char[] cs=name.toCharArray();
        if (!(cs[0] >= 'A' && cs[0] <= 'Z')) {
            return name;
        }
        cs[0]+=32;
        return String.valueOf(cs);
    }

}
